package allenja.cp2011.MVC;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class MessageView implements Observer {
	private JFrame frame;
	private JTextField portNumberTextField;
	private JTextField serverIP;
	private JButton btnStart;
	private JButton btnStop;
	private JScrollPane scrollPane;
	private JTextArea textArea;
	
	public MessageView() {
		initialize();
	}
	
	// Create the GUI and display the IP Address of the local machine
	private void initialize() {
		frame = new JFrame("Chat Server");
		frame.setBounds(100, 100, 600, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.addWindowListener(new CloseListener());
		
		JPanel panel = new JPanel();
		frame.getContentPane().add(panel, BorderLayout.SOUTH);
		
		JLabel lblServerIP = new JLabel("IP Address");
		panel.add(lblServerIP);
		serverIP = new JTextField();
		serverIP.setColumns(16);
		
		try {
			InetAddress address = InetAddress.getLocalHost();
			byte[] ipAddress = address.getAddress();
			int i = 4;
			String value = "";
			
			// Convert the raw IP Address to a readable string
			for(byte raw: ipAddress) {
				value += (raw & 0xFF);
				if (--i > 0) {
					value += ".";
				}
			}
			
			serverIP.setText(value);
			
		} catch (UnknownHostException e) {
			System.out.println("Unknown host error: " + e);
		}
		
		panel.add(serverIP);
		
		JLabel lblPort = new JLabel("Port");
		panel.add(lblPort);		
		portNumberTextField = new JTextField();
		portNumberTextField.setText("49696");
		panel.add(portNumberTextField);
		portNumberTextField.setColumns(10);
		
		btnStart = new JButton("Start");
		panel.add(btnStart);
		
		btnStop = new JButton("Stop");
		panel.add(btnStop);
		
		textArea = new JTextArea();
		textArea.setFont(new Font("Monospaced", Font.PLAIN, 14));
		textArea.setEditable(false);
		textArea.setTabSize(4);
		textArea.setColumns(50);
		
		scrollPane = new JScrollPane(textArea);
		
		frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
		
		frame.setVisible(true);
	}

	// Create the Observable update() call
	@Override
	public void update(Observable obs, Object obj) {
		textArea.append((String)obj.toString() + "\n");
		textArea.setCaretPosition(textArea.getText().length() - 1);
	}
	
	// Create the Controller ActionListeners
	public void addController(ActionListener messageController) {
		btnStart.addActionListener(messageController);
		btnStop.addActionListener(messageController);
	}
	
	// Close listener for the main window
	public static class CloseListener extends WindowAdapter {
		public void windowClosing(WindowEvent e) {
			e.getWindow().setVisible(false);
			System.exit(0);
		}
	}
}
